//
//  DeviceChartItemView.m
//  RuiHeApp
//
//  Created by Jun on 2022/11/25.
//

#import "DeviceChartItemView.h"
#import "DeviceChartFieldModel.h"

@implementation DeviceChartItemView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    [self setBackgroundColor:UIColor.whiteColor];
    [self.layer setCornerRadius:3];
    
    _markLab = [[QMUILabel alloc] init];
    [_markLab.layer setBorderWidth:1];
    [_markLab.layer setCornerRadius:2.0];
    [_markLab.layer setMasksToBounds:YES];
    [self addSubview:_markLab];
    
    _conentLab = [[QMUILabel alloc] init];
    [_conentLab setFont:[UIFont systemFontOfSize:12]];
    [_conentLab setTextAlignment:NSTextAlignmentLeft];
    [_conentLab setTextColor:FontColor(@"#000000")];
    [self addSubview:_conentLab];
    
    _btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_btn];
    
    [_markLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(10, 10));
        make.left.mas_offset(5);
        make.centerY.mas_offset(0);
    }];
    
    [_conentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.markLab.mas_right).offset(5);
        make.top.bottom.mas_offset(0);
        make.right.mas_offset(-5);
    }];
    
    [_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.top.bottom.mas_offset(0);
    }];

    //[_markLab.layer setBorderColor:FontColor(@"#3ECE80").CGColor];
    kWeakSelf;
    [[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf btnAction];
    }];
}

- (void)setModel:(DeviceChartFieldModel *)model{
    _model = model;
    if (_model) {
        NSString *content = model.alias;
        if (content.length == 0) {
            content = model.keyName;
        }
        [_conentLab setText:content.rh_localized];
        [_markLab setBackgroundColor:model.isShowLine?FontColor(_model.colorStr):[UIColor whiteColor]];
    }
}

- (void)btnAction{
    if (_model&&self.selectBlock) {
        BOOL result = self.selectBlock(_model);
        if (result) {
            [_markLab setBackgroundColor:_model.isShowLine?FontColor(_model.colorStr):[UIColor whiteColor]];
        }
    }
}

@end
